package com.example.task1.ui.fastfood;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.FastMyCollectBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class FastMyCollectAdapter extends BaseAdapter<FastMyCollectBean.RowsDTO> {

    private Context context;
    private List<FastMyCollectBean.RowsDTO> mData = new ArrayList<>();
    private ImageView image;
    private TextView tvName;
    private TextView tvIngredients;
    private Button btnRemove;

    @Override
    protected void setData(List<FastMyCollectBean.RowsDTO> rows) {
        mData = rows;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.fastmycollectlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<FastMyCollectBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, FastMyCollectBean.RowsDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, FastMyCollectBean.RowsDTO date, int position) {
        MyApplication.glide(image, date.getImgUrl());
        tvIngredients.setText(date.getIngredients());
        tvName.setText(date.getName());
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("dishesId", date.getId());
                map.put("newState", false);
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/kitchen-helper/dishes-collection", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("取消成功");
                        mData.remove(position);
                        notifyItemRemoved(position);
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    private void initView(View view) {
        image = view.findViewById(R.id.image);
        tvName = view.findViewById(R.id.tv_name);
        tvIngredients = view.findViewById(R.id.tv_ingredients);
        btnRemove = view.findViewById(R.id.btn_remove);
    }
}
